<template>
	<view>
		<view v-if="isLoading == true"><image class="loading" src="https://huajia-online-1316192331.cos.ap-guangzhou.myqcloud.com/static/loading-1.gif"></image></view>
		<view v-if="isLoading == false">
			<view class="point">我的积分:{{ point }}</view>
			<button class="order" size="mini" @click="myOrder">我的订单</button>
			<button class="editInfo" size="mini" @click="editAddressInfo">编辑收货地址</button>
			<uni-list direction="col" v-for="one in list">
				<uni-card class="card" :cover="one.cover">
					<image class="image" slot="cover" :src="one.cover"></image>
					<text class="uni-body">{{ one.name }}</text>
					<br />
					<text>所需积分:{{ one.point }}</text>
					<view>库存：{{ one.stock }}</view>
					<br />
					<button size="mini" class="btn" @click="exchange(one.id, one.name, one.point, one.cover)">兑换</button>
				</uni-card>
			</uni-list>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			page: 1,
			length: 10,
			list: [
				{
					cover: '',
					name: '',
					point: '',
					stock: '',
					id: ''
				}
			],
			name: '',
			phone: '',
			address: '',
			num: 1,
			point: '',
			isLoading: true
		};
	},
	onReachBottom: function() {
		let that = this;
		if (that.isLastPage) {
			return;
		}
		that.page = that.page + 1;
		that.loadMessageList(that);
	},
	onLoad(options) {
		let that = this;
		that.name = options.name;
		that.phone = options.phone;
		that.address = options.address;
		console.log(that.name);
		console.log(that.phone);
		console.log(that.address);
		that.loadMessageList(that);
		that.ajax(that.url.searchUserPoint, 'GET', null, function(resp) {
			that.point = resp.data.result;
			console.log(that.point);
		});
		setTimeout(function(resp) {
			that.isLoading = false;
		}, 400);
	},
	methods: {
		myOrder() {
			uni.navigateTo({
				url: '/pages/my_order/my_order'
			});
		},
		blur(e) {
			console.log('blur:', e);
		},
		focus(e) {
			console.log('focus:', e);
		},
		changeValue(value) {
			console.log('返回数值：', value);
		},
		exchange(id, name, point, img) {
			let that = this;
			if (that.name == null) {
				uni.navigateTo({
					url: '/pages/choose_address/choose_address'
				});
			}
			let data = {
				productId: id,
				productName: name,
				productPoint: point,
				productCount: 1,
				totalPoint: point,
				address: that.address,
				name: that.name,
				phone: that.phone,
				img: img
			};
			if (that.name != null) {
				uni.showModal({
					title: '提示信息',
					content: '是否确定兑换？',
					success: function(resp) {
						if (resp.confirm) {
							that.ajax(that.url.insertOrder, 'POST', data, function(resp) {
								uni.showToast({
									icon: 'none',
									title: '兑换成功'
								});
								setTimeout(function(resp) {
									uni.navigateTo({
										url: '/pages/my_order/my_order'
									});
								}, 800);
							});
						}
					}
				});
				console.log('兑换成功！');
			}
		},
		editAddressInfo() {
			uni.navigateTo({
				url: '/pages/choose_address/choose_address'
			});
		},
		loadMessageList: function(ref) {
			let that = this;
			let data = {
				page: ref.page,
				length: ref.length
			};
			ref.ajax(ref.url.searchAllProductByPage, 'POST', data, function(resp) {
				let result = resp.data.result;
				if (result == null || result.length == 0) {
					ref.isLastPage = true;
					ref.page = ref.page - 1;
					uni.showToast({
						icon: 'none',
						title: '已经到底了'
					});
				} else {
					if (ref.page == 1) {
						ref.list = [];
					}
					ref.list = ref.list.concat(result);
				}
			});
		}
	}
};
</script>

<style lang="less">
@import url('exchange.less');
</style>
